package com.chuzhiyun.chengguoxy.feature.internship.leave;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

import com.chuzhiyun.chengguoxy.R;
import com.chuzhiyun.chengguoxy.databinding.ActivityClockHomeBinding;
import com.chuzhiyun.chengguoxy.databinding.ActivityLeaveHomeBinding;
import com.chuzhiyun.chengguoxy.feature.base.BaseActivity;
import com.chuzhiyun.chengguoxy.feature.internship.clockin.ClockHomeActivity;
import com.chuzhiyun.chengguoxy.feature.internship.clockin.ClockInFragment;
import com.chuzhiyun.chengguoxy.feature.internship.clockin.ClockInStatisticsFragment;
import com.winton.bottomnavigationview.NavigationView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("FieldCanBeLocal")
public class LeaveHomeActivity extends BaseActivity<ActivityLeaveHomeBinding> {
    private List<Fragment> fragments;// used for ViewPager adapter
    private LeaveHomeActivity.VpAdapter mAdapter;

    @Override
    public void initView(View view) {

        fragments = new ArrayList<>(2);
        LeaveInputFragment fragment0 = new LeaveInputFragment();
        LeaveListFragment fragment1 = new LeaveListFragment();
        fragments.add(fragment0);
        fragments.add(fragment1);

        List<NavigationView.Model> tabs = new ArrayList<>();
        tabs.add(new NavigationView.Model.Builder(R.mipmap.leave_select,R.mipmap.leave_normal).title("请假").build());
        tabs.add(new NavigationView.Model.Builder(R.mipmap.statistics_select,R.mipmap.statistics_normal).title("记录").build());
        viewBinding.nv.setItems(tabs);
        viewBinding.nv.setOnTabSelectedListener(new NavigationView.OnTabSelectedListener() {
            @Override
            public void selected(int i, NavigationView.Model model) {
                viewBinding.vp.setCurrentItem(i);
            }

            @Override
            public void unselected(int i, NavigationView.Model model) {

            }
        });

        viewBinding.nv.build();
        viewBinding.nv.reminder(0,false,"");
        viewBinding.nv.reminder(1,false,"");

        mAdapter = new LeaveHomeActivity.VpAdapter(getSupportFragmentManager(), fragments);
        viewBinding.vp.setAdapter(mAdapter);
        viewBinding.vp.setOffscreenPageLimit(3);
        viewBinding.vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewBinding.nv.check(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void setCurrentIndex(int index){
        viewBinding.vp.setCurrentItem(index%2);
    }

    @SuppressWarnings("deprecation")
    private static class VpAdapter extends FragmentPagerAdapter {
        private final List<Fragment> data;
        VpAdapter(FragmentManager fm, List<Fragment> data) {
            super(fm);
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @NotNull
        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }
    }


    @Override
    public void initData() {

    }

    @Override
    public void setListener() {

    }

    @Override
    protected ActivityLeaveHomeBinding getViewBinding() {
        return ActivityLeaveHomeBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }
}